package com.hxy.example.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;

public class DisplayUtils {
    public static boolean isLandscape(Context context) {
        return context.getResources().getConfiguration().orientation == 2;
    }

    public static boolean isPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == 1;
    }

    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    public static float getScreenDensity(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }

    public static int getScreenDensityDPI(Context context) {
        return context.getResources().getDisplayMetrics().densityDpi;
    }

//    public static int getTitleBarHeight(Activity activity) {
//        int titleBarHeight = activity.getWindow().findViewById(16908290).getTop() - getStatusBarHeight(activity);
//        return titleBarHeight < 0 ? 0 : titleBarHeight;
//    }

    public static int getStatusBarHeight(Activity activity) {
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        return rect.top;
    }

    public static int getStatusBarHeight2(Activity activity) {
        int statusBarHeight = getStatusBarHeight(activity);
        if (statusBarHeight != 0) {
            return statusBarHeight;
        }
        try {
            Class<?> localClass = Class.forName("com.android.internal.R$dimen");
            return activity.getResources().getDimensionPixelSize(Integer.parseInt(localClass.getField("status_bar_height").get(localClass.newInstance()).toString()));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return statusBarHeight;
        } catch (IllegalAccessException e2) {
            e2.printStackTrace();
            return statusBarHeight;
        } catch (InstantiationException e3) {
            e3.printStackTrace();
            return statusBarHeight;
        } catch (NumberFormatException e4) {
            e4.printStackTrace();
            return statusBarHeight;
        } catch (IllegalArgumentException e5) {
            e5.printStackTrace();
            return statusBarHeight;
        } catch (SecurityException e6) {
            e6.printStackTrace();
            return statusBarHeight;
        } catch (NoSuchFieldException e7) {
            e7.printStackTrace();
            return statusBarHeight;
        }
    }

    public static int dip2px(Context context, float dp) {
        if (context == null) {
            return -1;
        }
        return (int) (dipToPx(context, dp) + 0.5f);
    }

    private static float dipToPx(Context context, float dp) {
        if (context == null) {
            return -1.0f;
        }
        return dp * context.getResources().getDisplayMetrics().density;
    }

    public static int px2dip(Context context, float px) {
        if (context == null) {
            return -1;
        }
        return (int) (pxToDip(context, px) + 0.5f);
    }

    private static float pxToDip(Context context, float px) {
        if (context == null) {
            return -1.0f;
        }
        return px / context.getResources().getDisplayMetrics().density;
    }

    public static int px2sp(Context context, float px) {
        return (int) (pxToSp(context, px) + 0.5f);
    }

    private static float pxToSp(Context context, float px) {
        return px / context.getResources().getDisplayMetrics().scaledDensity;
    }

    public static int sp2px(Context context, float sp) {
        return (int) (spToPx(context, sp) + 0.5f);
    }

    private static float spToPx(Context context, float sp) {
        return sp * context.getResources().getDisplayMetrics().scaledDensity;
    }
}
